var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var bodyParser = require('body-parser');

var app = express();

app.all('*', function (req, res, next) {
    console.log('ports', '3002端口');
    // res.header("Access-Control-Allow-Origin", "http://localhost:63342");//接收特定域名
    res.header('Access-Control-Allow-Origin', req.headers.origin || '*');
    res.header('Access-Control-Allow-Headers', 'Content-Type,Content-Length, Authorization,Origin,Accept,X-Requested-With');
    res.header('Access-Control-Allow-Methods', '*');
    res.header('Access-Control-Allow-Credentials', true);
    res.header('X-Powered-By', ' 3.2.1');
    // 响应头为json的话，访问图片出现乱码,不应该强制设置，返回数据类型会自动匹配
    // res.header('Content-Type', 'application/json;charset=utf-8');
    if (req.method === 'OPTIONS') {//头部携带自定义字段会发送与请求如token   OPTIONS
        res.sendStatus(200);
    } else {
        next();
    }
});


app.use(logger('dev'));
app.use(express.json());
app.use(bodyParser.json());//body-parser 解析json格式数据
app.use(bodyParser.urlencoded({extended: false}));//bodyParser 处理post请求
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));


var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var uploadRouter = require('./common/uploadFiles');

app.use('/', indexRouter);
app.use('/index', indexRouter);

app.use('/userLogin', usersRouter);
app.use('/userLogin/more', usersRouter);
app.use('/userLogin/getToken', usersRouter);
app.use('/userLogin/moreHandle', usersRouter);

app.use('/uploadFiles', uploadRouter);//必须要有一个/uploadFiles标志，要不然找不到入口出现404
app.use('/uploadFiles/res', uploadRouter);

module.exports = app;
